﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using SmartMD.DataModel;

namespace Duoease.ContainerModel.Entity
{
    public class ShelfModel : BaseModel
    {
        #region 构造函数
        public ShelfModel()
        {
            this.Title = "架子";
            this.ElementType = "shelf";
            this.TagName = "ShelfModel";
        }
        public ShelfModel(string tile)
        {
            this.Title = tile;
            this.ElementType = "shelf";
            this.TagName = "ShelfModel";
        }
        public ShelfModel(int row, int column, string tile)
        {
            Row = row;
            Column = column;
            this.Title = tile;
            this.ElementType = "shelf";
            this.TagName = "ShelfModel";
        }
        public ShelfModel(int row, int column, Color surfaceColor, int alpha, string tile)
        {
            Row = row;
            Column = column;
            SurfaceColor = surfaceColor;
            Alpha = alpha;
            this.Title = tile;
            this.ElementType = "shelf";
            this.TagName = "ShelfModel";
        }
        public ShelfModel(int row, int column, Color surfaceColor, int alpha, Color lineColor, int lineWidth, string tile)
        {
            Row = row;
            Column = column;
            SurfaceColor = surfaceColor;
            Alpha = alpha;
            LineColor = lineColor;
            LineWidth = lineWidth;
            this.Title = tile;
            this.ElementType = "shelf";
            this.TagName = "ShelfModel";
        }
        #endregion
        /**
        * 功能：解析模型文件
        * 
        * */
        public override void ParseXml(XmlNode xn)
        {
            if (xn == null)
            {
                return;
            }
            base.ParseXml(xn);

        }

        /**
       * 功能：刷新控件
       * 
       * */
        public override void Refresh()
        {
            this.View.middlelabel.Text = this.Name + this.View.FridgeModel.ID + ID;

           /* if (this.View.IsCreatNewModel)
            {
                this.View.docmodel.ConstructXml(this, ModelStyle.ShelfModel, this.View.ctnNo);
            }*/

            this.StoredPercent = new double[Row, Column];  //架子存放状态
           this.View.ShelfWidth = this.panel.Width;
           this.View.ShelfHeight = this.panel.Height;

            ContainerWidth = this.panel.Width;
            ContainerHeight = this.panel.Height;
            // AffixToLabel(ModelStyle.ShelfModel, ShelfWidth, ShelfHeight);
            AffixToLabel(this.View.middlelabel, this.View.ShelfWidth, this.View.ShelfHeight);
            this.panel.Refresh();
        }

        /**
        * 功能：构造模型文件
        * 
        * */
        public override XmlElement ConstructXml(XmlDocument doc, string tagName, XmlElement element = null)
        {
            return base.ConstructXml(doc, tagName, element);
        }

        public override void Draw()
        {
            if (Row < 1 || Column < 1)
            {
                return;
            }
            g.Clear(BackGround);
            this.Location.X = ContainerWidth / (Column + 2);
            this.Location.Y = ContainerHeight / (Row + 2);
            if (UnitWidth == 0)
            {
                UnitWidth = ContainerWidth / (Column + 2);
            }
            if (UnitHeight == 0)
            {
                UnitHeight = ContainerHeight / (Row + 2);
            }
            JoinIceModel(g, Location, Row, Column, UnitWidth, UnitHeight, Alpha, SurfaceColor, LineColor, LineWidth);
        }

        public override void AffixToLabel(Label oldLabel, int width, int height)
        {
            Label newLabel = new Label();
            this.panel.Controls.Clear();
            this.panel.Controls.Add(oldLabel);
            for (int i = 0; i < this.Row; i++)
            {
                for (int j = 0; j < this.Column; j++)
                {
                    newLabel = new Label();
                    newLabel.Width = 42;
                    newLabel.Text = ((i + 1) * 10 + j + 1).ToString();
                    newLabel.Font = new Font(newLabel.Font.FontFamily, 20, FontStyle.Bold);

                    newLabel.MouseDown += new MouseEventHandler(this.View.DnewLabel_MouseDown);
                    newLabel.ForeColor = Color.Black;
                    newLabel.BackColor = Color.Transparent;
                    this.ContainerWidth = width;
                    this.ContainerHeight = height;
                    newLabel.Location = new Point(Location.X + UnitWidth * j + UnitWidth / 2 - 10, Location.Y + UnitHeight * i + UnitHeight / 2 - 10);
                    // newLabel.Parent = this.splitContainer1.Panel1;
                    this.panel.Controls.Add(newLabel);
                }
            }
        }


        /**
         * 功能：鼠标点击事件
         * 
         * */
        public event MouseEventHandler OnMouseClickEvent;
        public override void OnMouseClickHandler(object sender, MouseEventArgs e)
        {
            Point p = new Point(e.X, e.Y);
            int x = 0;
            int y = 0;
            Point origin = Location;

            if (e.X >= origin.X && e.X <= origin.X + UnitWidth * Column && e.Y >= origin.Y && e.Y <= origin.Y + UnitHeight * Row)
            {
                for (int i = 0; i < Column; i++)
                {
                    if (p.X >= origin.X + i * UnitWidth && p.X <= origin.X + (i + 1) * UnitWidth)
                    {
                        x = i + 1;
                        break;
                    }
                }
                for (int i = 0; i < Row; i++)
                {
                    if (p.Y >= origin.Y + i * UnitHeight && p.Y <= origin.Y + (i + 1) * UnitHeight)
                    {
                        y = i + 1;
                        break;
                    }
                }
                //g = this.View.g;
                Draw();
                Render(g, new Point(origin.X + (x - 1) * UnitWidth, origin.Y + (y - 1) * UnitHeight), UnitWidth, UnitHeight, Alpha, Color.FromArgb(255, Color.Blue));

                this.View.MouseRow = y;
                this.View.MouseColumn = x;
                if (this.OnMouseClickEvent != null)
                {
                    this.OnMouseClickEvent(sender, e);
                }

                // Remove(ModelStyle.BoxModel);
                if (this.View.BoxModel != null)
                {
                    this.View.BoxModel.RefreshLabel("盒子");
                }
                string BName = "BoxModel" +ID + "-" + (y * 10 + x).ToString();
                XmlNode node = this.View.docmodel.Xml.SelectSingleNode("Container");
                BoxModel boxmodel = null;
                foreach (XmlNode xn in node)//遍历
                {
                    if (xn.Name == BName)
                    {
                        boxmodel = new BoxModel();
                        boxmodel.ParseXml(xn);
                        break;
                    }
                }
                this.View.BoxModel = boxmodel;


               
              
            }
        }

    }
}
